
#open(BUFF,"file_improper_snps.txt");
#while(<BUFF>)
#{
#        chomp($_);
#	$hashy{$_}="";
#}
#open(WRB1,">impropersnps_before");
#open(WRB2,">impropersnps_after");
open(BUFF,"snps_ill.txt");
while(<BUFF>)
{
	chomp($_);
	@a = split(" ",$_);
	$hash{$a[1]} ="";
}
open(BUFF,"final.dosage");
open(WRBUFF,">ILL_8000_after_concordance");
while(<BUFF>)
{
        chomp($_);
	@a = split(" ",$_);
	if(exists($hash{$a[0]}))
	{
		$snp = shift(@a);
		$gnt1 = shift(@a);
		$gnt2 = shift(@a);
		if($gnt2 eq "-")
		{
			$gnt2 = $gnt1;
		}
		#print WRBUFF $_."\n";
		@final=();
		#print @a."\n";
		#400 for affy and for illumina 400 -800	
		for($i=0;$i<@a;$i++)
		{
			$val = 2*$a[$i];
			$i++;
			$val =$val+$a[$i];
			
			if($val >= 1.5)
			{
				$val1 = "$gnt1 $gnt1";
				push(@final,$val1);		
			}
			elsif($val <= 0.5)
			{
				$val1 = "$gnt2 $gnt2";
				push(@final,$val1);
			}
			else
			{
				$val1 = "$gnt2 $gnt1";
				push(@final,$val1);
			}
		}
		$_ = join(" ",@final);
		#if(exists($hashy{$snp}))
		#{
		#	print WRB2 "$snp $gnt1 $gnt2 $_\n";
		#}
		$hash{$snp} = $_;
		#print $hash{$snp}."\n";
	}
}
open(BUFF,"ILL_200samples.tped");
while(<BUFF>)
{
        chomp($_);
        @a = split(" ",$_);
	shift(@a);
	$snp = shift(@a);
	$total_concord = 0;
	 #if(exists($hashy{$snp}))
         #       {
         #               print WRB1 "$snp $gnt1 $gnt2 $_\n";
         #       }

	if(exists($hash{$snp}))
	{
		shift(@a);
		shift(@a);
		@b = split(" ",$hash{$snp});
		$sum = 0;
		if(@a == @b)
		{
			for($i=0;$i<@a;$i++)
			{
				$m = $i;
				$i++;
				$n = $i;
				if($a[$m] ne "0" && $a[$n] ne "0")
				{
					$sum++;
				}
				if(($a[$m] eq $b[$m] && $a[$n] eq $b[$n]) || ($a[$m] eq $b[$n] && $a[$n] eq $b[$m]))
				{
					$total_concord++;		
				} 
			}
		}
		else
		{
			die "number doesn't match\n";
		}
		if($total_concord == 0)
		{
			$val = 0;
		}
		else
		{
			$val = $total_concord/$sum;
		}
		print WRBUFF $snp."\t".$val."\n";		
	}
}

